package com.outfood.api.controller;

import com.alibaba.fastjson.JSON;
import com.outfood.common.common.replay.ReplyCode;
import com.outfood.common.common.response.RestResponse;
import com.outfood.config.WxGzhConfig;
import com.outfood.domain.User;
import com.outfood.service.IUserService;
import com.outfood.service.impl.SeleniumServiceImpl;
import com.outfood.utils.TokenUtils;
import com.rys.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.openqa.selenium.WebDriver;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfood-platform
 * @Package com.outfood.api.controller
 * @Date 2024-05-15 15:48
 * @Version 1.0
 */
@RequestMapping
@Controller
@Log4j2
public class IndexController {


    @Resource
    RedisTemplate redisTemplate;

    @ResponseBody
    @ApiOperation("心跳检测")
    @GetMapping("/heart")
    public RestResponse heart(){
        redisTemplate.opsForValue().get("5c76ce71-51cd-47c4-be1e-066e700da533");
        return new RestResponse(ReplyCode.SYS.SUCCESS,"成功");
    }

    @ResponseBody
    @ApiOperation("根据门店关闭webdriver")
    @GetMapping("/setWebDriverQuit")
    public RestResponse setWebDriverQuit(@ApiParam("店铺ID")@RequestParam  String shopId){
//        try {
//            WebDriver webDriver = SeleniumServiceImpl.webDriverMap.get(shopId);
//            webDriver.close();
//            webDriver.quit();
//        } catch (Exception e) {
//            log.info("关闭浏览器失败");
//        }
        return new RestResponse(ReplyCode.SYS.SUCCESS,"关闭成功");
    }
    @ResponseBody
    @RequestMapping("/MP_verify_pReekkswHWtxiVmr.txt")
    public String index(){
        return "pReekkswHWtxiVmr";
    }

    @ResponseBody
    @RequestMapping("/Tfzpf7fklh.txt")
    public String index4(){
        return "2a597c20abe876f43b5a96466c8eb1ad";
    }

    @ResponseBody
    @RequestMapping("/ff6db76e4445ecdfdd19263ce3f2747d.txt")
    public String index2(){
        return "0b17da79a4afed6f3d449168fa7dd14a15a1a87b";
    }

    @RequestMapping("/bind/{userid}")
    public String createCode(@PathVariable(name = "userid") Long userid){
        String url = wxGzhConfig.getAuthonUrl();
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx757dbc7e2900864d&redirect_uri="+url+"?userid="+userid+"&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect";

    }

    TokenUtils tokenUtils;

    @Resource
    IUserService userService;
    @Resource
    RestTemplate restTemplate;
    @Resource
    WxGzhConfig wxGzhConfig;

    @RequestMapping("getopenId")
    public String getOpenId(String code,Long userid){
        String appid=wxGzhConfig.getAppid();
        String secret = wxGzhConfig.getSecret();
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret+ "&code=" + code + "&grant_type=authorization_code"
                , String.class);
        AccessTokenResult accessTokenResult = JSON.parseObject(forEntity.getBody(), AccessTokenResult.class);
        log.info(forEntity.getBody());
        User user = userService.get(userid);
        if(StringUtils.isBlank(user.getGzhOpenId())){
            user.setGzhOpenId(accessTokenResult.getOpenid());
            userService.update(user.getId(),user,"authoerz");
        }

        return "redirect:https://mp.weixin.qq.com/s?__biz=Mzg5Mzc2MjgxNg==&mid=2247483653&idx=1&sn=81b75163ea9516bcc53c284836d8d05c&chksm=c028ad6af75f247cfc98ab690f47001ca8684e99b0627972d4be2978dcbb6600dfa541eb4968#rd";
    }

    public static class AccessTokenResult {

        private String access_token;
        private int expires_in;
        private String refresh_token;
        private String openid;
        private String scope;
        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }
        public String getAccess_token() {
            return access_token;
        }

        public void setExpires_in(int expires_in) {
            this.expires_in = expires_in;
        }
        public int getExpires_in() {
            return expires_in;
        }

        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }
        public String getRefresh_token() {
            return refresh_token;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }
        public String getOpenid() {
            return openid;
        }

        public void setScope(String scope) {
            this.scope = scope;
        }
        public String getScope() {
            return scope;
        }

    }

}
